//
//  HeapSort.swift
//  Chapter3LinkedList
//
//  Created by TRS-Mobile on 2021/9/3.
//

import Foundation

// 堆排序（最大堆或者最小堆排序）

extension Heap {
    
    func sorted() -> [E] {
        var heap = Heap(sort: sort, elements: elements)
        for idx in heap.elements.indices.reversed() {
            print("-->:\(heap.elements)")
            heap.elements.swapAt(0, idx)
            heap.siftDown(from: 0, upTo: idx)
        }
        return heap.elements
    }
    
}
